package com.mall.b2c.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.domain.delivery.DeliveryMethodDomain;
import com.mall.b2c.domain.order.RefundDomain;
import com.mall.b2c.domain.order.RefundLogDomain;
import com.mall.b2c.enums.RefundStatus;
import com.mall.b2c.service.DeliveryMethodService;
import com.mall.b2c.service.RefundLogService;
import com.mall.b2c.service.RefundService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Message;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.exception.NoticeException;
import com.mall.common.util.EnumUtils;
import com.mall.common.validation.annotation.IntRangeFieldValidator;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;
import com.mall.common.web.Servlets;

@Controller
@RequestMapping("/admin/b2c/refund")
public class RefundController extends BaseController {
	@Resource private RefundService refundService;
	@Resource private DeliveryMethodService deliveryMethodService;
	@Resource private RefundLogService refundLogService;

	@RequestMapping("/list")
	public String list(Model model){
		model.addAttribute("refundStatuses", EnumUtils.enumProp2I18nList(RefundStatus.class));
		return "/admin/b2c/refund/list"; 
	}
	
	@RequestMapping("/queryData")
	@ResponseBody
	public Pager queryData(Pager pager, HttpServletRequest request){
		Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(filterParams);
		pager = refundService.findPage(pager, filters);
		return JsonDataGridHelper.createJSONData(pager);
	}

	@RequestMapping("/detail/{id}")
	public String deal(@PathVariable Long id, Model model){
		RefundDomain refund = refundService.find(id);
		if(refund == null){
			throw new NoticeException("单据不存在");
		}
		model.addAttribute("refund", refund);
		return "/admin/b2c/refund/detail";
	}

	@Validations(
		requiredFields = { 
				@RequiredFieldValidator(fieldName = "id", message = "单据不存在!"),
				@RequiredFieldValidator(fieldName = "amount", message = "金额不能为空")
		},
		intRangeFields = {
				@IntRangeFieldValidator(fieldName="amount",min="0",message="金额不正确")
		}
	)
	@RequestMapping("/confirm")
	@ResponseBody
	public Message confirm(Long id, BigDecimal amount){
		refundService.confirm(id, amount, getUsername());
		return successMessage;
	}

	@Validations(
		requiredFields = { 
				@RequiredFieldValidator(fieldName = "id", message = "单据不存在!")
		}
	)
	@RequestMapping("/refuse")
	@ResponseBody
	public Message refuse(Long id){
		refundService.refuse(new Long[]{id}, getUsername());
		return successMessage;
	}

	@Validations(
		requiredFields = { 
				@RequiredFieldValidator(fieldName = "id", message = "单据不存在!"),
				@RequiredFieldValidator(fieldName = "amount", message = "退款金额不能为空")
		},
		intRangeFields = {
				@IntRangeFieldValidator(fieldName="amount",min="0",message="退款金额不正确")
		}
	)
	@RequestMapping("/confirmReject")
	@ResponseBody
	public Message confirmReject(Long id, BigDecimal amount){
		refundService.approve(id, amount, getUsername());
		return successMessage;
	}

	@RequestMapping("/change/{id}")
	public String change(@PathVariable Long id, Model model){
		RefundDomain refund = refundService.find(id);
		if(refund == null){
			throw new NoticeException("单据不存在");
		}
		if(!refund.getIsChange()){
			throw new NoticeException("换货单无需换货");
		}
		if(refund.getRefundStatus() != RefundStatus.confirm){
			throw new NoticeException("换货单状态不正确");
		}
		model.addAttribute("refund", refund);
		model.addAttribute("methods", deliveryMethodService.getDeliveryMethodList(true));
		return "/admin/b2c/refund/change";
	}

	@Validations(
		requiredFields = { 
				@RequiredFieldValidator(fieldName = "id", message = "换货单不存在!"),
				@RequiredFieldValidator(fieldName = "deliveryMethod", message = "配送方式不存在"),
				@RequiredFieldValidator(fieldName = "shipFee", message = "配送费用不能为空")
		},
		requiredStrings = {
				@RequiredStringValidator(fieldName="logisticsNo",message="快递单号不正确")
		},
		intRangeFields = {
				@IntRangeFieldValidator(fieldName="shipFee",min="0",message="退款金额不正确")
		}
	)
	@RequestMapping("/change")
	@ResponseBody
	public Message change(Long id, String logisticsNo, Long deliveryMethod, BigDecimal shipFee){
		RefundDomain refund = refundService.find(id);
		if(refund == null){
			throw new NoticeException("换货单不存在");
		}
		if(!refund.getIsChange()){
			throw new NoticeException("该单据不是换货单");
		}
		if(refund.getRefundStatus() != RefundStatus.confirm){
			throw new NoticeException("换货单状态不正确");
		}
		DeliveryMethodDomain method = deliveryMethodService.find(deliveryMethod);
		if(method == null){
			throw new NoticeException("配送方式不正确");
		}
		refund.setLogisticsNo(logisticsNo);
		refund.setDeliveryMethodName(method.getName());
		refund.setShipFee(shipFee);
		refund.setRefundStatus(RefundStatus.send);
		refundService.update(refund);

		RefundLogDomain log = new RefundLogDomain("售后已发货，待您收货确认", getUsername(), "系统", refund);
		refundLogService.persist(log);
		return successMessage;
	}
	
	@RequestMapping("/finish/{id}")
	public String finish(@PathVariable Long id, Model model){
		RefundDomain refund = refundService.find(id);
		if(refund == null){
			throw new NoticeException("退款单不存在");
		}
		if(refund.getIsChange()){
			throw new NoticeException("退款单无需退款");
		}
		if(refund.getRefundStatus() != RefundStatus.confirm){
			throw new NoticeException("退款单状态不正确");
		}
		model.addAttribute("refund", refund);
		return "/admin/b2c/refund/finish";
	}

	@Validations(
		requiredFields = { 
				@RequiredFieldValidator(fieldName = "id", message = "退款单不存在!")
		}
	)
	@RequestMapping("/finish")
	@ResponseBody
	public Message finish(Long id){
		refundService.finish(id, getUsername());
		return successMessage;
	}

	@RequestMapping("/log")
	public String log(){
		return "/admin/b2c/refund/log";
	}
	
	@RequestMapping("/queryLogData")
	@ResponseBody
	public Pager queryLogData(Pager pager, HttpServletRequest request){
		Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(filterParams);
		pager = refundLogService.findPage(pager, filters);
		return JsonDataGridHelper.createJSONData(pager);
	}
	
}
